<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="(value) => emit('update:modelValue', value)"
    :title="isEdit ? '编辑应急演练' : '新增应急演练'"
    width="900px"
    :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="120px"
      size="default"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="演练编号" prop="drillNo">
            <el-input v-model="form.drillNo" placeholder="请输入演练编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="演练名称" prop="drillName">
            <el-input v-model="form.drillName" placeholder="请输入演练名称" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="演练类型" prop="drillType">
            <el-select v-model="form.drillType" placeholder="选择演练类型" style="width: 100%">
              <el-option label="桌面演练" value="desktop" />
              <el-option label="功能演练" value="functional" />
              <el-option label="全面演练" value="comprehensive" />
              <el-option label="实战演练" value="actual_combat" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="演练级别" prop="drillLevel">
            <el-select v-model="form.drillLevel" placeholder="选择演练级别" style="width: 100%">
              <el-option label="国家级" value="national" />
              <el-option label="省级" value="provincial" />
              <el-option label="市级" value="municipal" />
              <el-option label="县级" value="county" />
              <el-option label="企业级" value="enterprise" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="演练日期" prop="drillDate">
            <el-date-picker
              v-model="form.drillDate"
              type="date"
              placeholder="选择演练日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="演练时长" prop="drillDuration">
            <el-input v-model="form.drillDuration" placeholder="请输入演练时长(小时)" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="演练地点" prop="drillLocation">
            <el-input v-model="form.drillLocation" placeholder="请输入演练地点" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="组织单位" prop="organizationUnit">
            <el-input v-model="form.organizationUnit" placeholder="请输入组织单位" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="总指挥" prop="commander">
            <el-input v-model="form.commander" placeholder="请输入总指挥" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="参演人数" prop="participantCount">
            <el-input-number v-model="form.participantCount" :min="1" placeholder="参演人数" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="观摩人数" prop="observerCount">
            <el-input-number v-model="form.observerCount" :min="0" placeholder="观摩人数" style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="演练目标" prop="drillObjective">
            <el-input
              v-model="form.drillObjective"
              type="textarea"
              :rows="3"
              placeholder="请输入演练目标"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="演练场景" prop="drillScenario">
            <el-input
              v-model="form.drillScenario"
              type="textarea"
              :rows="4"
              placeholder="请输入演练场景描述"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="演练科目" prop="drillSubjects">
            <el-input
              v-model="form.drillSubjects"
              type="textarea"
              :rows="4"
              placeholder="请输入演练科目内容"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="参演单位" prop="participatingUnits">
            <el-input
              v-model="form.participatingUnits"
              type="textarea"
              :rows="3"
              placeholder="请输入参演单位"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="装备物资" prop="equipmentMaterials">
            <el-input
              v-model="form.equipmentMaterials"
              type="textarea"
              :rows="4"
              placeholder="请输入装备物资清单"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="演练过程" prop="drillProcess">
            <el-input
              v-model="form.drillProcess"
              type="textarea"
              :rows="6"
              placeholder="请输入演练过程记录"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="演练效果" prop="drillEffect">
            <el-input
              v-model="form.drillEffect"
              type="textarea"
              :rows="4"
              placeholder="请输入演练效果评估"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="存在问题" prop="existingProblems">
            <el-input
              v-model="form.existingProblems"
              type="textarea"
              :rows="4"
              placeholder="请输入发现的问题"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="改进措施" prop="improvementMeasures">
            <el-input
              v-model="form.improvementMeasures"
              type="textarea"
              :rows="4"
              placeholder="请输入改进措施"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="评估人员" prop="evaluator">
            <el-input v-model="form.evaluator" placeholder="请输入评估人员" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="评估日期" prop="evaluationDate">
            <el-date-picker
              v-model="form.evaluationDate"
              type="date"
              placeholder="选择评估日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :rows="3"
              placeholder="请输入备注信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="emit('update:modelValue', false)">取消</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'

interface Props {
  modelValue: boolean
  drillData?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  drillData: null
})

const emit = defineEmits<Emits>()

const formRef = ref<FormInstance>()
const isEdit = ref(false)

const form = reactive({
  drillNo: '',
  drillName: '',
  drillType: '',
  drillLevel: '',
  drillDate: '',
  drillDuration: '',
  drillLocation: '',
  organizationUnit: '',
  commander: '',
  contactPhone: '',
  participantCount: 0,
  observerCount: 0,
  drillObjective: '',
  drillScenario: '',
  drillSubjects: '',
  participatingUnits: '',
  equipmentMaterials: '',
  drillProcess: '',
  drillEffect: '',
  existingProblems: '',
  improvementMeasures: '',
  evaluator: '',
  evaluationDate: '',
  remark: ''
})

const rules: FormRules = {
  drillNo: [
    { required: true, message: '请输入演练编号', trigger: 'blur' }
  ],
  drillName: [
    { required: true, message: '请输入演练名称', trigger: 'blur' }
  ],
  drillType: [
    { required: true, message: '请选择演练类型', trigger: 'change' }
  ],
  drillLevel: [
    { required: true, message: '请选择演练级别', trigger: 'change' }
  ],
  drillDate: [
    { required: true, message: '请选择演练日期', trigger: 'change' }
  ],
  drillLocation: [
    { required: true, message: '请输入演练地点', trigger: 'blur' }
  ],
  organizationUnit: [
    { required: true, message: '请输入组织单位', trigger: 'blur' }
  ],
  commander: [
    { required: true, message: '请输入总指挥', trigger: 'blur' }
  ],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  drillObjective: [
    { required: true, message: '请输入演练目标', trigger: 'blur' }
  ]
}

// 重置表单
const resetForm = () => {
  Object.assign(form, {
    drillNo: '',
    drillName: '',
    drillType: '',
    drillLevel: '',
    drillDate: '',
    drillDuration: '',
    drillLocation: '',
    organizationUnit: '',
    commander: '',
    contactPhone: '',
    participantCount: 0,
    observerCount: 0,
    drillObjective: '',
    drillScenario: '',
    drillSubjects: '',
    participatingUnits: '',
    equipmentMaterials: '',
    drillProcess: '',
    drillEffect: '',
    existingProblems: '',
    improvementMeasures: '',
    evaluator: '',
    evaluationDate: '',
    remark: ''
  })
  formRef.value?.clearValidate()
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    // 这里应该调用API保存数据
    console.log('提交表单数据:', form)
    
    ElMessage.success(isEdit.value ? '修改成功' : '添加成功')
    emit('success')
    emit('update:modelValue', false)
  } catch (error) {
    ElMessage.error('请检查表单填写是否正确')
  }
}

// 监听弹窗打开，初始化表单数据
watch(() => props.modelValue, (newVal) => {
  if (newVal) {
    if (props.drillData) {
      isEdit.value = true
      Object.assign(form, props.drillData)
    } else {
      isEdit.value = false
      resetForm()
    }
  }
}, { immediate: true })
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>